package huaabdelmartin;

import java.io.IOException;

public class ClassManager {

	/**instance variables
	 * 
	 */
	private ReadAndStoreInput inputObject;
	private CreateReports reportsObject;
	private WriteOutPut outputObject; 

	/**
	 * Constructor
	 * Will run the methods inside it when a new ClassManager object is created 
	 * @throws VehicleRegFormatException 
	 */
	public ClassManager() {

		instantiateInputClass();
		instantiateReportsClass();
		instantiateOutputClass();
	}

	/**
	 * Creates ReadAndStoreInput object, then run object methods which read details in 
	 * from files and store in various Collections inside that object
	 * @throws VehicleRegFormatException 
	 */
	private void instantiateInputClass() {
		inputObject = new ReadAndStoreInput();
		try {
			try {
				inputObject.readAndStoreTaxisCollection();
			} catch (DuplicateDriverIdException d) {
				d.printStackTrace();
			}
			inputObject.readAndStoreDestinationsCollection();
			inputObject.readAndStoreJourneysList2015();
			inputObject.readAndStoreDestinationsList2014();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Creates CreateReports object, then runs object methods which take data from
	 * the ReadAndStoreInput object and arrange/transfer them to collections inside 
	 * the CreateReports object in such a way that they are easier to 
	 * extract data from for the creation of the reports required for this coursework. 
	 * The methods which actually return the data in strings are in the same object,
	 * but are not called here except for printing to console, but instead in the next method for writing to files
	 */
	private void instantiateReportsClass(){
		reportsObject = CreateReports.getInstance( inputObject );//CreateReports follows Singleton pattern :)
		reportsObject.sortExpensiveCheapJourneyCollection();
		reportsObject.populateDriverDestinationsCollection();
		reportsObject.populateDestinations20142015Collection();

		//print the reports to the console
		System.out.println(reportsObject.returnDestination20142015Report());
		System.out.println(reportsObject.returnDriverDestinationsReport());
		System.out.println(reportsObject.returnExpensiveCheapJourneyReport());
	}

	/**
	 * Creates WriteOutput object and calls methods in the object which write out to file. These methods access
	 * the reportsObject methods in order to get the reports as strings, then write them to files.
	 */
	private void instantiateOutputClass(){
		outputObject = new WriteOutPut( reportsObject );
		outputObject.writeExpensiveCheapJourneyReport();
		outputObject.writeDriverDestinationsReport();
		outputObject.writeDestination20142015Report();
	} 
} 